package com.shujia.wyh.day07.homework;

/*
        数组：查询块，增删慢
        增删慢的原因因为数组的长度一旦被创建就不能被改变

        合并数组操作：现有如下一个数组：int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}
        要求将以上数组中值为0的项去掉，将不为0的值存入一个新的数组，
        生成的新数组为： int newArr [] ={1,3,4,5,6,6,5,4,7,6,7,5}
 */
public class Test6 {
    public static void main(String[] args) {
        //定义旧数组
        int oldArr[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5};
        //统计不为0的元素有多少个
        int count = 0;
        for (int i = 0; i < oldArr.length; i++) {
            if (oldArr[i] != 0) {
                count++;
            }
        }
        //定义一个新数组
        int[] newArr = new int[count];
        //定义一个遍历来表示新数组的索引变化
        int index = 0;

        //遍历旧数组将不为0的数存入到新数组中
        for (int i = 0; i < oldArr.length; i++) {
            if (oldArr[i] != 0) {
                newArr[index] = oldArr[i];
                index++;
            }
        }

        //打印新数组
        for (int i = 0; i < count; i++) {
            if (i == 0) {
                System.out.print("[" + newArr[i] + ",");
            } else if (i == count - 1) {
                System.out.print(newArr[i] + "]");
            } else {
                System.out.print(newArr[i] + ",");
            }
        }


    }
}
